/*
 * ==========================================================================*\
 * | $Id$
 * |*-------------------------------------------------------------------------*|
 * | Copyright (C) 2009 Virginia Tech | | This file is part of CloudSpace. | |
 * CloudSpace is free software; you can redistribute it and/or modify | it under
 * the terms of the GNU General Public License as published | by the Free
 * Software Foundation; either version 3 of the License, or | (at your option)
 * any later version. | | CloudSpace is distributed in the hope that it will be
 * useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of |
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General
 * Public License for more details. | | You should have received a copy of the
 * GNU General Public License | along with CloudSpace; if not, see
 * <http://www.gnu.org/licenses/>.
 * \*==========================================================================
 */

package cloudspace.security;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;


// -------------------------------------------------------------------------
/**
 * A factory class that produces GrantedAuthority instances used to represent
 * CloudSpace groups. The instances are cached as flyweights.
 * 
 * @author Tony Allevato
 * @author Last changed by $Author$
 * @version $Revision$, $Date$
 */
public class GroupFactory
{
    // ~ Constructors ..........................................................

    // ----------------------------------------------------------
    /**
     * Prevents instantiation of this static class.
     */
    private GroupFactory()
    {
        // Static class; prevent instantiation.
    }

    // ~ Constants .............................................................

    /**
     * A default role that includes all CloudSpace users. Spring Security
     * requires that all users have at least one role, so this exists even for
     * users who do not belong to other groups. It will not appear in the group
     * list that is viewable by system administrators, nor will it be persisted
     * to the configuration files.
     */
    public static final GrantedAuthority DEFAULT_ROLE = new GrantedAuthorityImpl( "__cloudspace_default_role" );

    /**
     * The group that represents people who are given administrator-level access
     * to CloudSpace.
     */
    public static final GrantedAuthority ADMIN_GROUP = groupWithName( "admin" );


    // ~ Methods ...............................................................

    // ----------------------------------------------------------
    /**
     * Gets a GrantedAuthority object for the particular group name.
     * 
     * @param group
     *            the CloudSpace group name
     * @return a GrantedAuthority representing the group
     */
    public static GrantedAuthority groupWithName( String group )
    {
        if ( cache == null )
        {
            cache = new HashMap<String, GrantedAuthority>();
        }

        if ( cache.containsKey( group ) )
        {
            return cache.get( group );
        }
        else
        {
            GrantedAuthority authority = new GrantedAuthorityImpl( group );
            cache.put( group, authority );
            return authority;
        }
    }

    // ~ Static/instance variables .............................................

    private static Map<String, GrantedAuthority> cache;
}
